﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernate;
using BotonJXC.DAO;
using BotonJXC.Entity;
using BotonJXC.BLL;

namespace BotonJXC.Forms.Controls
{
    public partial class AssumeModeControl : BaseInfoControl
    {
        AssumeModeBLL _assumeModeBll = new AssumeModeBLL();
        string _assumeModeName;
        public AssumeModeControl()
        {
            InitializeComponent();
        }

        private void kbSearch_Click(object sender, EventArgs e)
        {
            _assumeModeName = ktbAssumeModeName.Text;
            pager1.PageNo = 1;
            LoadData();
        }

        private void AssumeModeControl_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            PageModel<AssumeMode> pageModel = _assumeModeBll.Find(_assumeModeName, pager1.PageStart, pager1.PageSize);
            pager1.TotalCount = pageModel.TotalCount;
            kdgvAssumeMode.DataSource = new BindingList<AssumeMode>(pageModel.Results);
        }

        public override IList<object> SelectedItems
        {
            get
            {
                IList<object> selectedItems = new List<object>();
                foreach (DataGridViewRow r in kdgvAssumeMode.SelectedRows)
                {
                    selectedItems.Add(r.DataBoundItem);
                }
                return selectedItems;
            }
        }

        public override void AddInfo()
        {

        }
        public override void SaveInfo()
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvAssumeMode.Rows)
                {
                    AssumeMode u = r.DataBoundItem as AssumeMode;
                    if (u == null)
                        continue;
                    if (u.AssumeModeId == 0)
                    {
                        _assumeModeBll.Save(u);
                    }
                    else if ("1".Equals(r.Tag) && u.AssumeModeId != 0)
                    {
                        _assumeModeBll.Update(u);
                    }
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
        }
        public override void DeleteInfo()
        {
            IList<AssumeMode> assumeModes = (IList<AssumeMode>)kdgvAssumeMode.DataSource;
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvAssumeMode.SelectedRows)
                {
                    AssumeMode item = r.DataBoundItem as AssumeMode;
                    if (item != null)
                    {
                        _assumeModeBll.Delete(item);
                    }
                    assumeModes.RemoveAt(r.Index);
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
            kdgvAssumeMode.Refresh();
        }

        private void kdgvAssumeMode_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
                kdgvAssumeMode.Rows[e.RowIndex].Tag = "1";
        }

        private void pager1_PageChanged(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
